<template>
  <div>
    <el-tree
      ref="tree"
      :data="permission"
      :props="{label: 'name'}"
      default-expand-all
      show-checkbox
      node-key="id"
      check-strictly
    />
    <el-button @click="$emit('cloed')">取消</el-button>
    <el-button @click="hSubmit">确认</el-button>
  </div>
</template>
<script>
import { getPermissionList } from '@/api/permissons'
import { tranListToTreeData } from '@/utils'
import { assignPermission, getRoleDetail } from '@/api/settings'
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      permission: []
    }
  },
  created() {
    this.loadpermission()
    this.loadperDeil()
  },
  methods: {
    async loadpermission() {
      const res = await getPermissionList()
      this.permission = tranListToTreeData(res.data)
    },
    async hSubmit() {
      const ids = this.$refs.tree.getCheckedKeys()
      try {
        const res = await assignPermission({
          id: this.id,
          permIds: ids
        })
        this.$message.success(res.message)
        this.$emit('cloed')
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    async loadperDeil() {
      const res = await getRoleDetail(this.id)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    }
  }
}
</script>

